//
//  CustomExcetion.m
//  HappyStore
//
//  Created by 点点充 on 16/1/16.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "CustomExcetion.h"

@implementation CustomExcetion


+ (NSDate *)dateFromString:(NSString *)dateString
{
    NSTimeZone *currentDateTimeZone = [NSTimeZone defaultTimeZone];
    NSDateFormatter *currentDateFormat = [[NSDateFormatter alloc]init];
    [currentDateFormat setTimeZone:currentDateTimeZone];
    [currentDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    
    NSDate *destDate = [currentDateFormat dateFromString:dateString];
    
    return destDate;
    
}

+ (NSString *)prettyDateWithReference:(NSDate *)reference
{
    NSString *suffix = @"前";
    
    float different = [reference timeIntervalSinceDate:[NSDate date]];
    if (different < 0) {
        different = -different;
        //        suffix = @"从现在开始";
    }
    
    // days = different / (24 * 60 * 60), take the floor value
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
    int weeks  = (int)ceil(dayDifferent / 7);
    int months = (int)ceil(dayDifferent / 30);
    int years  = (int)ceil(dayDifferent / 365);
    
    // It belongs to today
    if (dayDifferent <= 0) {
        // lower than 60 seconds
        if (different < 60) {
            return @"刚刚";
        }
        
        // lower than 120 seconds => one minute and lower than 60 seconds
        if (different < 120) {
            return [NSString stringWithFormat:@"1分钟%@", suffix];
        }
        
        // lower than 60 minutes
        if (different < 66 * 60) {//39600
            return [NSString stringWithFormat:@"%d分钟%@", (int)floor(different / 60), suffix];
        }
        
        // lower than 60 * 2 minutes => one hour and lower than 60 minutes
        if (different < 7200) {
            return [NSString stringWithFormat:@"1小时%@", suffix];
        }
        
        // lower than one day
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d小时%@", (int)floor(different / 3600), suffix];
        }
    }
    // lower than one week
    else if (days < 7) {
        return [NSString stringWithFormat:@"%d天%@%@", days, days == 1 ? @"" : @"", suffix];
    }
    // lager than one week but lower than a month
    else if (weeks < 4) {
        return [NSString stringWithFormat:@"%d周%@%@", weeks, weeks == 1 ? @"" : @"", suffix];
    }
    // lager than a month and lower than a year
    else if (months < 12) {
        return [NSString stringWithFormat:@"%d月%@%@", months, months == 1 ? @"" : @"", suffix];
    }
    // lager than a year
    else {
        return [NSString stringWithFormat:@"%d年%@%@", years, years == 1 ? @"" : @"", suffix];
    }
    
    return self.description;
}



+ (NSString *)comperNumber:(NSString *)number
{
    CGFloat num1 = [number floatValue];
    NSString *strNum = @"";
    
    if (num1 >= 100000000) {
        strNum = [NSString stringWithFormat:@"%.0f亿",num1/100000000];
    }else if (num1 >= 10000 && num1 < 100000000) {//大于等于1W小于1Y
        strNum = [NSString stringWithFormat:@"%.0f万",num1/10000];
    }else {
        //小于1万
        strNum = [NSString stringWithFormat:@"%.0f元",num1];
    }
    if ([strNum isEqualToString:@"0元"]) {
        strNum = @"面议";
    }
    return strNum;
}

@end
